angular.module('service.ssoauth', [
        "ngResource"
    ])
    .service('AuthService', AuthService);
AuthService.$inject = ['$resource'];
function AuthService($resource) {
    // 获取验证码
    this.getcode = function (phone,type, success_func, error_func) {
        if(type=='register'){
            this.reset_code(phone, success_func, error_func);
        }else if(type=='rePassword'){
            this.forgot_code(phone, success_func, error_func);
        }else if(type=='paymentPass'){
            this.payment_code(phone, success_func, error_func);
        }else if(type=='wxbind'){
            this.wxbind_code(phone, success_func, error_func);
        }
    };
    //GET /user/{id} 获取用户接口
    this.getuser = function (id,success_func, error_func) {
        var resource = $resource(uc_baseurl + 'user/:id');
        return resource.get({id:id}, success_func, error_func);
    };

    // forgotpassword 发送短信获取验证码
    this.forgot_code = function (phone, successFn, errorFn) {
        var resource = $resource(sso_baseurl + 'user/reset/applycode/:phone');
        return resource.get({ phone: phone }, successFn, errorFn)
    };
    // resetpassword  忘记密码获取验证码
    this.reset_code = function (phone, successFn, errorFn) {
        var resource = $resource(sso_baseurl + "user/reset/registcode/:phone");
        return resource.get({ phone: phone}, successFn, errorFn);
    };
    // resetpassword  微信绑定获取验证码
    this.wxbind_code = function (phone, successFn, errorFn) {
        var resource = $resource(sso_baseurl + "user/reset/wxbindcode/:phone");
        return resource.get({ phone: phone}, successFn, errorFn);
    };
    //忘记支付密码获取验证码
    this.payment_code = function (phone, successFn, errorFn) {
        var GetPhoneCode = $resource(uc_baseurl + 'user/sendcode/:acount');
        var acount = "cf_xwm";
        GetPhoneCode.get({ acount: acount, phone: phone }, successFn, errorFn);
    };
    // resetpassword  发送短信告知重置的密码
    this.retrieve = function (user, successFn, errorFn) {
        var resource = $resource(sso_baseurl + "user/reset/bycode/:phone");
        var _self = this;
        return resource.get({ phone: user.phone, code: user.code,password: user.newPassword}, function(userinfo){
            var login_user = {
                name:user.phone,
                password:user.newPassword
            };
            _self.userlogin(login_user, successFn, errorFn);
        }, errorFn);
    };
    // 注册
    this.register = function (user, success_func, error_func) {
        var UserRegist = $resource(sso_baseurl + 'user/register');
        //var UserRegist = $resource(sso_baseurl + 'user/openregister');
        var xwmRegist = $resource(uc_baseurl+'user/add');
        var userreg = {
            username: user.nickname,
            password: hex_hmac_md5("fjswxxjsyxgstyrz", user.password),
            phone: user.phone,
            email: user.email,
            code: user.code,
            role:""
        };
        return UserRegist.save( userreg, function(data){
            var xwmuser = {
                "name": user.phone,
                "password": user.password,
                "logo": "",
                "phone": user.phone,
                "nickname": user.nickname,
                "sign": 11
            };
            xwmRegist.save({userid:data.userid},xwmuser, success_func, error_func);
        }, error_func);
    };
    // 登陆
    this.userlogin = function (user, success_func, error_func) {
        var resource = $resource(sso_baseurl + 'login', {}, {
            login: {
                method: 'POST',
                //dataType: 'JSONP',//here
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                transformRequest: function (data, headersGetter) {
                    var str = [];
                    for (var d in data) {
                        str.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
                    }
                    return str.join("&");
                }
            }
        });
        //
        var loginuser = {
            //"password": user.password
            "username": user.name,
            "password": hex_hmac_md5("fjswxxjsyxgstyrz", user.password)
        };
        var acctype = judgeAccountType(user.name);
        if (acctype != "1") {
            loginuser["acctype"] = acctype;
        }
        //
        var login_ret = resource.login({}, loginuser,function (userinfo) {
            var uc_user = $resource(uc_baseurl+"user/:userid");
            uc_user.get({userid:userinfo.userid},success_func,error_func);
        },error_func);
        return login_ret;
    };
}
